// pages/task_add/task_add.js
import WxValidate from '../../utils/WxValidate.js';
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projectStatus: 2, // 要求属于：运行中的项目
    projectArr: [],
    projectIndex: null,
    projectId: null,
    projectName: null,
    taskTypeArr: [
      {value: 1, label: '单人'}, 
      {value: 2, label: '多人领取'}, 
      {value: 3, label: '多人指派'}
    ],
    taskTypeIndex: null,
    taskType: null,
    taskTypeLabel: null,
    taskName: null,
    taskScore: 0,
    taskDesc: null,
    userId: null,
    userVip: 0, // 默认普通用户
    urlParamProjectId: 0, // 项目页面传递过来的值，用于指定默认项目
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: wx.getStorageSync('userId'),
      urlParamProjectId: options.projectId? options.projectId: null
    });
    this.initListData();
    this.initValidate();
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '新建任务'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '新建任务'
    }
  },

  /**
  * 初始化列表数据
  */
  initListData() {
    // 初始化当前负责人正在运行的项目，下拉框数据
    let param = {
      projectStatus: this.data.projectStatus,
      projectUserId: this.data.userId
    }
    if (this.data.urlParamProjectId) {
      param['projectId'] = this.data.urlParamProjectId
    }

    request.GET('/project/findProjectList', param).then(res => {
      let resArr = res.data;
      this.setData({
        projectArr: resArr
      })

      // 结果只有一条数据的时候，默认展示第一个
      if (resArr!=null && resArr.length==1) {
        this.setData({
          projectIndex: 0,
          projectId: resArr[0].projectId,
          projectName: resArr[0].projectName
        })
      }
    })
  },

  /**
   * 初始化表单校验
   */
  initValidate() {
    const rules = {
      projectIndex: {
        required: true
      },
      taskTypeIndex: {
        required: true
      },
      taskName: {
        required: true
      },
      taskScore: {
        required: true,
        digits: true
      }
    }

    const messages = {
      projectIndex: {
        required: '请选择所属项目'
      },
      taskTypeIndex: {
        required: '请选择任务类型'
      },
      taskName: {
        required: '请输入任务名称'
      },
      taskScore: {
        required: '请输入任务奖励积分',
        digits: '任务奖励积分，只能输入整数'
      }
    }
    this.WxValidate = new WxValidate(rules, messages);
  },

  /**
   * 项目选择
   */
  bindProjectIdPickerChange(e) {
    let selectIndex = e.detail.value;
    let projectObj = this.data.projectArr[selectIndex];

    if (projectObj != undefined) {
      this.setData({
        projectIndex: selectIndex,
        projectId: projectObj.projectId,
        projectName: projectObj.projectName
      })
    } else {
      // 由于picker默认值是0，避免当前用户一个项目都没有时，选择确认后。绕开必填校验
      this.setData({
        projectIndex: null
      })
    }
  },

  /**
   * 任务类型选择
   */
  bindTaskTypePickerChange(e) {

    let selectIndex = e.detail.value;
    let taskObj = this.data.taskTypeArr[selectIndex];

    this.setData({
      taskTypeIndex: selectIndex,
      taskType: taskObj.value,
      taskTypeLabel: taskObj.label
    })
  },

  formSubmit(e) {
    let params = e.detail.value;
    if (!this.WxValidate.checkForm(params)) {
      let error = this.WxValidate.errorList[0];
      wx.showModal({
        title: '温馨提示',
        content: error.msg,
        showCancel: false
      })
      return false
    }
    
    // 添加需要的字段与值
    params.projectId = this.data.projectId;
    params.taskType = this.data.taskType;
    params.userId = this.data.userId;
    request.POST('/task/createTask', params).then(res => {
      if (res.statusCode==200) {
        setTimeout(function() {
          wx.navigateBack({
            delta: 1,
          })
        }, 500)
      } else {
        wx.showToast({
          title: '保存异常失败',
          icon: 'error',
          duration: 1500
        })
      }
    })
  },

})